<template>
  <div v-show="editShow">
    <div class="container">
      <el-row :gutter="20">
        <div class="clearfix">
          <div class="crumbs">
            <i class="el-icon-back"></i>
            <el-button type="text" @click="goBack">返回</el-button>
          </div>
          <div class="btn-save">
            <el-button type="primary" @click="saveClick('ruleForm')">保存</el-button>
          </div>
        </div>
        <div class="form-box">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="120px"
            class="eidt-el-form"
          >
            <el-form-item label="名称" prop="name">
              <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="展品描述" prop="remark">
              <el-input v-model="ruleForm.remark" type="textarea" :rows="3"></el-input>
            </el-form-item>
            <UploadImg @uploadImg="getUploadImg" :editImg="editImg" :imgTitle="imgTitle"></UploadImg>
            <el-form-item label="状态" prop="state">
              <el-select v-model="ruleForm.state" placeholder="请选择" @change="getChangeState">
                <el-option label="未审核" value="1"></el-option>
                <el-option label="已审核" value="2"></el-option>
                <el-option label="已拒绝" value="3"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="展商">
              <el-select v-model="ruleForm.exhibitorId" placeholder="选择展商" >
                <el-option
                  v-for="(type,index) in exhibitors"
                  :label="type.name+','+type.company"
                  :key="index"
                  :value="type.exhibitorId">
                  <div @click="select(type)">{{type.name+','+type.company}}</div>
                </el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </div>
      </el-row>
    </div>
  </div>
</template>

<script>
import UploadImg from "../../public/UploadImg";

export default {
  name: "editexhibiorspersonal",
  data() {
    return {
      rules: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }]
      },
      ruleForm: {
        name: "",
        state:"",
        remark:"",
        url:'',
        projectId: "",
        exhibitorId:"",
        booth:"",
        company:"",
        phone:""
      },
      exhibitors:[],
      editImg: "",
      exhibitorProduct_Id: "",
      imgTitle: "上传图片"
    };
  },
  props: {
    editShow: {
      type: Boolean,
      default: false
    },
    editData: {
      type: Object
    }
  },
  created() {
    var project_Id = localStorage.getItem("project_Id");
    this.projectId = project_Id;
    this.$axios.get("/Exhibitor/Project/"+this.projectId).then(res => {
          if (res.data.hasError == false) {
            this.exhibitors=[...res.data.data]
          } 
        });
  },
  watch: {
    editData: function() {
      const item = this.editData;
      this.ruleForm = item;
      this.ruleForm.state=item.state.toString();
      this.exhibitorProduct_Id = item.exhibitorProductId;
      this.editImg = item.url;
    }
  },
  methods: {
    select(item){
      console.log(item);
      this.ruleForm.exhibitorId=item.exhibitorId;
      this.ruleForm.company=item.company;
      this.ruleForm.booth=item.booth;
      this.ruleForm.phone=item.phone;
    },
    saveClick(ruleForm) {
      this.$refs[ruleForm].validate(valid => {
        if (valid) {
          if (this.exhibitorProduct_Id == "" || this.exhibitorProduct_Id == undefined) {
            this.$axios.post("/ExhibitorProduct/", this.ruleForm).then(res => {
              if (res.data.hasError == false) {
                this.$message.success("保存成功！");
                this.goBack();
                this.$refs[ruleForm].resetFields();
              } else {
                this.$message.error("保存失败！");
              }
            });
          } else {
            this.$axios
              .put("/ExhibitorProduct/" + this.exhibitorProduct_Id + "", this.ruleForm)
              .then(res => {
                if (res.data.hasError == false) {
                  this.$message.success("编辑成功！");
                  this.goBack();
                  this.$refs[ruleForm].resetFields();
                } else {
                  this.$message.error("编辑失败！");
                }
              });
          }
        } else {
          return false;
        }
      });
    },
    goBack() {
      this.$emit("backFunStu");
      this.exhibitorProduct_Id = "";
    },
    getUploadImg(imgSrc) {
      this.ruleForm.url = imgSrc;
    },
    getChangeState(value){
        this.ruleForm.state = value;
    },
    getChange(val){
      this.ruleForm.exhibitorId=val;
    }
  },
  components: {
    UploadImg
  }
};
</script>